﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace CPGM
{
    [Serializable]
    public class CPGMPacket
    {
        public string LocalIPAddress { get; set; }

        public byte[] Data { get; set; }

        public Type DataType { get; set; }

        public int DataLength { get; set; }

        public bool IsLast { get; set; }

        public int IsRetransmission { get; set; }

        public int ID { get; set; }

        public byte[] ConvertToByteArray()
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            return ms.ToArray();
        }

        public CPGMPacket LoadFromByteArray(byte[] b)
        {
            MemoryStream memStream = new MemoryStream();
            BinaryFormatter binForm = new BinaryFormatter();
            memStream.Write(b, 0, b.Length);
            memStream.Seek(0, SeekOrigin.Begin);
            CPGMPacket obj = (CPGMPacket)binForm.Deserialize(memStream);
            LocalIPAddress = obj.LocalIPAddress;
            Data = obj.Data;
            DataType = obj.DataType;
            DataLength = obj.DataLength;
            ID = obj.ID;
            IsLast = obj.IsLast;
            return obj;
        }
    }
}
